package me.zhengjie.modules.web.service.pay;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 王宇
 */
@Component
public class PayGatewayFactory implements ApplicationContextAware {

    private static final Map<Integer, IPayGatewayService> GATEWAY_MAP = new HashMap<>();

    public static IPayGatewayService orderType(Integer orderType) {
        return GATEWAY_MAP.get(orderType);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, IPayGatewayService> beans = applicationContext.getBeansOfType(IPayGatewayService.class);
        beans.values().forEach(bean -> GATEWAY_MAP.put(bean.orderType(), bean));
    }
}
